/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler.jpa;

import com.camachosanchez.bean.FormasPago;
import com.camachosanchez.controler.jpa.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.camachosanchez.bean.Vehiculos;
import java.util.ArrayList;
import java.util.List;
import com.camachosanchez.bean.Neumaticos;
import com.camachosanchez.util.JPAUtil;

/**
 *
 * @author Rolando
 */
public class FormasPagoJpaController implements Serializable {

    public FormasPagoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return JPAUtil.getEntityManagerFactory().createEntityManager();
    }

    public void create(FormasPago formasPago) {
        if (formasPago.getVehiculosList() == null) {
            formasPago.setVehiculosList(new ArrayList<Vehiculos>());
        }
        if (formasPago.getNeumaticosList() == null) {
            formasPago.setNeumaticosList(new ArrayList<Neumaticos>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Vehiculos> attachedVehiculosList = new ArrayList<Vehiculos>();
            for (Vehiculos vehiculosListVehiculosToAttach : formasPago.getVehiculosList()) {
                vehiculosListVehiculosToAttach = em.getReference(vehiculosListVehiculosToAttach.getClass(), vehiculosListVehiculosToAttach.getId());
                attachedVehiculosList.add(vehiculosListVehiculosToAttach);
            }
            formasPago.setVehiculosList(attachedVehiculosList);
            List<Neumaticos> attachedNeumaticosList = new ArrayList<Neumaticos>();
            for (Neumaticos neumaticosListNeumaticosToAttach : formasPago.getNeumaticosList()) {
                neumaticosListNeumaticosToAttach = em.getReference(neumaticosListNeumaticosToAttach.getClass(), neumaticosListNeumaticosToAttach.getId());
                attachedNeumaticosList.add(neumaticosListNeumaticosToAttach);
            }
            formasPago.setNeumaticosList(attachedNeumaticosList);
            em.persist(formasPago);
            for (Vehiculos vehiculosListVehiculos : formasPago.getVehiculosList()) {
                FormasPago oldIdFormaPagoOfVehiculosListVehiculos = vehiculosListVehiculos.getIdFormaPago();
                vehiculosListVehiculos.setIdFormaPago(formasPago);
                vehiculosListVehiculos = em.merge(vehiculosListVehiculos);
                if (oldIdFormaPagoOfVehiculosListVehiculos != null) {
                    oldIdFormaPagoOfVehiculosListVehiculos.getVehiculosList().remove(vehiculosListVehiculos);
                    oldIdFormaPagoOfVehiculosListVehiculos = em.merge(oldIdFormaPagoOfVehiculosListVehiculos);
                }
            }
            for (Neumaticos neumaticosListNeumaticos : formasPago.getNeumaticosList()) {
                FormasPago oldIdFormaPagoOfNeumaticosListNeumaticos = neumaticosListNeumaticos.getIdFormaPago();
                neumaticosListNeumaticos.setIdFormaPago(formasPago);
                neumaticosListNeumaticos = em.merge(neumaticosListNeumaticos);
                if (oldIdFormaPagoOfNeumaticosListNeumaticos != null) {
                    oldIdFormaPagoOfNeumaticosListNeumaticos.getNeumaticosList().remove(neumaticosListNeumaticos);
                    oldIdFormaPagoOfNeumaticosListNeumaticos = em.merge(oldIdFormaPagoOfNeumaticosListNeumaticos);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FormasPago formasPago) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FormasPago persistentFormasPago = em.find(FormasPago.class, formasPago.getId());
            List<Vehiculos> vehiculosListOld = persistentFormasPago.getVehiculosList();
            List<Vehiculos> vehiculosListNew = formasPago.getVehiculosList();
            List<Neumaticos> neumaticosListOld = persistentFormasPago.getNeumaticosList();
            List<Neumaticos> neumaticosListNew = formasPago.getNeumaticosList();
            List<Vehiculos> attachedVehiculosListNew = new ArrayList<Vehiculos>();
            for (Vehiculos vehiculosListNewVehiculosToAttach : vehiculosListNew) {
                vehiculosListNewVehiculosToAttach = em.getReference(vehiculosListNewVehiculosToAttach.getClass(), vehiculosListNewVehiculosToAttach.getId());
                attachedVehiculosListNew.add(vehiculosListNewVehiculosToAttach);
            }
            vehiculosListNew = attachedVehiculosListNew;
            formasPago.setVehiculosList(vehiculosListNew);
            List<Neumaticos> attachedNeumaticosListNew = new ArrayList<Neumaticos>();
            for (Neumaticos neumaticosListNewNeumaticosToAttach : neumaticosListNew) {
                neumaticosListNewNeumaticosToAttach = em.getReference(neumaticosListNewNeumaticosToAttach.getClass(), neumaticosListNewNeumaticosToAttach.getId());
                attachedNeumaticosListNew.add(neumaticosListNewNeumaticosToAttach);
            }
            neumaticosListNew = attachedNeumaticosListNew;
            formasPago.setNeumaticosList(neumaticosListNew);
            formasPago = em.merge(formasPago);
            for (Vehiculos vehiculosListOldVehiculos : vehiculosListOld) {
                if (!vehiculosListNew.contains(vehiculosListOldVehiculos)) {
                    vehiculosListOldVehiculos.setIdFormaPago(null);
                    vehiculosListOldVehiculos = em.merge(vehiculosListOldVehiculos);
                }
            }
            for (Vehiculos vehiculosListNewVehiculos : vehiculosListNew) {
                if (!vehiculosListOld.contains(vehiculosListNewVehiculos)) {
                    FormasPago oldIdFormaPagoOfVehiculosListNewVehiculos = vehiculosListNewVehiculos.getIdFormaPago();
                    vehiculosListNewVehiculos.setIdFormaPago(formasPago);
                    vehiculosListNewVehiculos = em.merge(vehiculosListNewVehiculos);
                    if (oldIdFormaPagoOfVehiculosListNewVehiculos != null && !oldIdFormaPagoOfVehiculosListNewVehiculos.equals(formasPago)) {
                        oldIdFormaPagoOfVehiculosListNewVehiculos.getVehiculosList().remove(vehiculosListNewVehiculos);
                        oldIdFormaPagoOfVehiculosListNewVehiculos = em.merge(oldIdFormaPagoOfVehiculosListNewVehiculos);
                    }
                }
            }
            for (Neumaticos neumaticosListOldNeumaticos : neumaticosListOld) {
                if (!neumaticosListNew.contains(neumaticosListOldNeumaticos)) {
                    neumaticosListOldNeumaticos.setIdFormaPago(null);
                    neumaticosListOldNeumaticos = em.merge(neumaticosListOldNeumaticos);
                }
            }
            for (Neumaticos neumaticosListNewNeumaticos : neumaticosListNew) {
                if (!neumaticosListOld.contains(neumaticosListNewNeumaticos)) {
                    FormasPago oldIdFormaPagoOfNeumaticosListNewNeumaticos = neumaticosListNewNeumaticos.getIdFormaPago();
                    neumaticosListNewNeumaticos.setIdFormaPago(formasPago);
                    neumaticosListNewNeumaticos = em.merge(neumaticosListNewNeumaticos);
                    if (oldIdFormaPagoOfNeumaticosListNewNeumaticos != null && !oldIdFormaPagoOfNeumaticosListNewNeumaticos.equals(formasPago)) {
                        oldIdFormaPagoOfNeumaticosListNewNeumaticos.getNeumaticosList().remove(neumaticosListNewNeumaticos);
                        oldIdFormaPagoOfNeumaticosListNewNeumaticos = em.merge(oldIdFormaPagoOfNeumaticosListNewNeumaticos);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = formasPago.getId();
                if (findFormasPago(id) == null) {
                    throw new NonexistentEntityException("The formasPago with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FormasPago formasPago;
            try {
                formasPago = em.getReference(FormasPago.class, id);
                formasPago.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The formasPago with id " + id + " no longer exists.", enfe);
            }
            List<Vehiculos> vehiculosList = formasPago.getVehiculosList();
            for (Vehiculos vehiculosListVehiculos : vehiculosList) {
                vehiculosListVehiculos.setIdFormaPago(null);
                vehiculosListVehiculos = em.merge(vehiculosListVehiculos);
            }
            List<Neumaticos> neumaticosList = formasPago.getNeumaticosList();
            for (Neumaticos neumaticosListNeumaticos : neumaticosList) {
                neumaticosListNeumaticos.setIdFormaPago(null);
                neumaticosListNeumaticos = em.merge(neumaticosListNeumaticos);
            }
            em.remove(formasPago);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FormasPago> findFormasPagoEntities() {
        return findFormasPagoEntities(true, -1, -1);
    }

    public List<FormasPago> findFormasPagoEntities(int maxResults, int firstResult) {
        return findFormasPagoEntities(false, maxResults, firstResult);
    }

    private List<FormasPago> findFormasPagoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from FormasPago as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FormasPago findFormasPago(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FormasPago.class, id);
        } finally {
            em.close();
        }
    }

    public int getFormasPagoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from FormasPago as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
